from django import template
register=template.Library()
from django.utils.safestring import mark_safe
from web import models
@register.simple_tag
def page(page_obj,page_url):
    pre_page='<li class="page-pre"><a href="/%s/?page=%s">&lt;</a></li>'%(page_url,page_obj.number-1)
    pre_page_dis='<li class="page-pre disabled"><a>&lt;</a></li>'
    next_page='<li class="page-next"> <a href="/%s/?page=%s">&gt;</a></li>'%(page_url,page_obj.number+1)
    next_page_dis='<li class="page-next disabled"> <a>&gt;</a></li>'
    page_eml=[]
    if page_obj.has_previous():
        page_eml.append(pre_page)
    else:
        page_eml.append(pre_page_dis)
    per_page=3
    if page_obj.paginator.num_pages < per_page*2+1:
        start_page=1
        end_page=page_obj.paginator.num_pages+1
    else:
        if page_obj.number + per_page > page_obj.paginator.num_pages:
            end_page=page_obj.paginator.num_pages+1
            start_page=page_obj.paginator.num_pages-per_page*2
        else:
            if page_obj.number -per_page <= 0:
                start_page=1
                end_page=per_page*2+2
            else:
                start_page=page_obj.number-per_page
                end_page=page_obj.number+per_page+1
    for i in range(start_page,end_page):
        if page_obj.number == i:
            eml='<li class="page-next active"> <a href="/%s/?page=%s">%s</a></li>'%(page_url,i,i)
        else:
            eml='<li class="page-next "> <a href="/%s/?page=%s">%s</a></li>'%(page_url,i,i)
        page_eml.append(eml)
    if page_obj.has_next():
        page_eml.append(next_page)
    else:
        page_eml.append(next_page_dis)
    go="""
    <div class="col-sm-2 table-toolbar-right">
        <input type="text"    id="go">
        <button onclick="return go()" class="btn btn-purple"><i class="demo-pli-add icon-fw"></i>GO</button>
        <a class="btn btn-purple" href="/%s/?page=%s">末页</a>
    </div>
    """%(page_url,page_obj.paginator.num_pages)
    page_eml.append(go)
    res=''.join(page_eml)
    return mark_safe(res)

@register.filter
def Get_status(asset_id):
    obj=models.Asset.objects.filter(id=asset_id).first()
    if obj.asset_status_set.filter(asset_status=0):
        return 0
    else:
        return 1
